<?php

#chat.php
#
#Part of chatboss project
#
#POST the outputObject directly from the client , returns object.
#

include 'queryengine.php';
include 'functions.php';

#add actionids here
global $outputObject;
$outputObject = new protocolObject();

if(isset($_POST['inputObject']))
{
	$inputObject = json_decode((string)$_POST['inputObject']);
	
	if(gettype($inputObject))
	{
		switch($inputObject->actionId)
		{
			case 1:
				sendMessage($inputObject);
				break;
			case 2:
				getMessage($inputObject);
				break;
			case 3:
				addUser($inputObject);
				break;
			case 4:
				delUser($inputObject);
				break;
			default:
				noAction($inputObject);
				break;
		}
		

		sendDebugInfo($outputObject);
		echo json_encode($outputObject);
	}
	else
	{
		$outputObject->giveExitStatus(1,"The input type is incorrect. Ensure you are using an object.");
		echo json_encode($outputObject);
	}

	
}
#error based on script input
else
{
	$outputObject->giveExitStatus(1,"This script only takes one object input");
	echo json_encode($outputObject);
}

#
#send messages to user
#
# Requires:
#
# actionId = 1
#
# messageSender
#	The userID of the user who sent the message
#
# messageRecipient
#	The userID of the message recipient
#
# messageText
#	The text of the message itself
#
function sendMessage($inputObject)
{
	#post code
	global $outputObject;
	if($inputObject->messageSender != $inputObject->messageRecipient)
	{
		$ouputObject->actionId = $inputObject->actionId;
		$outputObject->messageSender = $inputObject->messageSender;
		$outputObject->messageRecipient = $inputObject->messageRecipient;
		$outputObject->messageText = $inputObject->messageText;
		$outputObject->senderQuery = "select count(*) from user where userId= '" . $outputObject->messageSender . "';";
		$outputObject->recipientQuery = "select count(*) from user where userId = '" . $outputObject->messageRecipient . "';";
		$outputObject->senderQueryResult = crowFather($outputObject->senderQuery,5);
		$outputObject->recipientQueryResult = crowFather($outputObject->recipientQuery,5);
		
		if($outputObject->senderQueryResult == 1 && $outputObject->recipientQueryResult == 1)
		{
			#both the users exist, we can send the message
			#Only continue if this is the case
			
			$cleanString = crowFather($outputObject->messageText,6);
			$outputObject->insertQuery = 'insert into message (messageSender,messageRecipient,messageText) values(' . $outputObject->messageSender . ',' . $outputObject->messageRecipient . ',' . $cleanString . ');';
			crowFather('insert into message (messageSender,messageRecipient,messageText) values(' . $outputObject->messageSender . ',' . $outputObject->messageRecipient . ',' . $cleanString . ');',3);
			$outputObject->giveExitStatus(0,"Both users exist and the message was sent");
		}
		if($outputObject->senderQueryResult == 0 || $outputObject->recipientQueryResult == 0)
		{
			#One or both users do not exist
			$outputObject->giveExitStatus(1,"The sender or recipient do not exist");
		}
		if($outputObject->senderQueryResult > 1 || $outputObject->recipientQueryResult > 1)
		{
			#One or both users have duplicate values in the user tables
			$outputObject->giveExitStatus(1,"The sender or recipient given has duplicate entries in the user table");
		}
	}
	else
	{
		#user is attempting to send to themselves
		$outputObject->giveExitStatus(1,"You are trying to send this message to yourself");
	}
}

#
#Get a list of messages sent to a user
#
# Requires:
#
# actionId = 2
#
# messageRecipient
#	The userId of the user who received the messages.
#

function getMessage($outputObject)
{
	global $outputObject;
	#get code
}

#
#Add a new user
#
# Requires:
#
# actionId = 3
#
# messageSender
#	The user you wish to add
#
# [userPrivilegeSet]
#		Default: 1
#	The user privileges you want to add to the account. Must correspond to a valid privilege set.
#
# [userDateOfBirth]
#		Default: Today's date
#	The user's date of birth. Will default to today's date and time if you don't set it
#
# [userDescription]
#	The user's brief description for the account. Not required at all.
#
function addUser($outputObject)
{
	#add user code
	global $outputObject;
}

#
#Delete an existing user
#
# Requires:
#
# actionId = 4
#
# messageSender
#	The user you wish to delete
#
function delUser($outputObject)
{
	#delete user code
	global $outputObject;
}

#An error checking function for object/input based errors
function noAction($outputObject)
{
	#error checking function for actionId based fail
	global $outputObject;
	if($outputObject->exitCode == 2)
	{
		#Exit status unchanged (nothing ran)
		$outputObject->giveExitStatus(1,"An exit status was not provided");
	}
	
	if($outputObject->actionId > 4)
	{
		$outputObject->giveExitStatus(1,"The action type was not one of the supported actions");
	}
}

?>